package mysql

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gen"
	"gorm.io/gen/field"
	"gozero-demo/internal/model/dao/model"
	"gozero-demo/internal/svc"
	"gozero-demo/internal/utils"
)

type CommonConfigRepo struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCommonConfigRepo(ctx context.Context, sCtx *svc.ServiceContext) *CommonConfigRepo {
	return &CommonConfigRepo{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: sCtx,
	}
}

func (r *CommonConfigRepo) GetCommonConfigInfo(id int64) (*model.CommonConfig, error) {
	m := r.svcCtx.Model.CommonConfig
	res, err := m.WithContext(r.ctx).Debug().Where(m.ID.Eq(id)).Where(m.IsDeleted.Eq(0)).First()
	if err != nil {
		logx.WithContext(r.ctx).Errorf("Failed to get data by id: %v", err)
		return nil, err
	}
	return res, nil
}

func (r *CommonConfigRepo) GetCommonConfigCount(queryEqualConditions map[string]interface{}, queryMoreConditions map[string]interface{}) (int64, error) {
	m := r.svcCtx.Model.CommonConfig
	queryEqualConditions["is_deleted"] = 0
	querys := m.WithContext(r.ctx).Debug()

	//基础查询条件
	querys = querys.Where(field.Attrs(queryEqualConditions))
	//更多查询条件
	if len(queryMoreConditions) > 0 {
		//todo your logic
	}

	return querys.Count()
}

func (r *CommonConfigRepo) GetCommonConfigListByConditions(queryEqualConditions map[string]interface{}, queryMoreConditions map[string]interface{}, page, pageSize int) ([]*model.CommonConfig, int64, error) {
	offset := (page - 1) * pageSize
	limit := pageSize
	m := r.svcCtx.Model.CommonConfig
	queryEqualConditions["is_deleted"] = 0
	querys := m.WithContext(r.ctx).Debug()

	//基础查询条件
	querys = querys.Where(field.Attrs(queryEqualConditions))
	//更多查询条件
	if len(queryMoreConditions) > 0 {
		//todo your logic
	}

	return querys.Order(m.ID.Desc()).FindByPage(offset, limit)
}

func (r *CommonConfigRepo) GetCommonConfigCreateTimeMapByIds(Ids []int64) (map[int64]string, error) {
	if len(Ids) == 0 {
		return nil, nil
	}
	Ids = utils.RemoveDuplicatesInt64Slice(Ids)

	m := r.svcCtx.Model.CommonConfig
	res, err := m.WithContext(r.ctx).Debug().Where(
		m.ID.In(Ids...),
		m.IsDeleted.Eq(0),
	).Find()
	if err != nil {
		logx.WithContext(r.ctx).Errorf("Failed to get create_time by ids: %v", err)
		return nil, err
	}

	mapData := make(map[int64]string, len(res))
	for _, v := range res {
		mapData[v.ID] = (v.CreateTime).Format("2006-01-02 15:04:05")
	}
	return mapData, nil
}

func (r *CommonConfigRepo) CreateCommonConfig(models *model.CommonConfig) (*model.CommonConfig, error) {
	m := r.svcCtx.Model.CommonConfig
	err := m.WithContext(r.ctx).Debug().Create(models)
	if err != nil {
		logx.WithContext(r.ctx).Errorf("Failed to insert data: %v", err)
		return nil, err
	}
	return models, nil
}

func (r *CommonConfigRepo) UpdateCommonConfig(models *model.CommonConfig, id int64) (gen.ResultInfo, error) {
	m := r.svcCtx.Model.CommonConfig
	res, err := m.WithContext(r.ctx).Debug().Where(m.ID.Eq(id)).Updates(models)
	if err != nil {
		logx.WithContext(r.ctx).Errorf("Failed to update data: %v", err)
		return res, err
	}
	return res, nil
}

func (r *CommonConfigRepo) DeleteCommonConfig(id int64) (gen.ResultInfo, error) {
	m := r.svcCtx.Model.CommonConfig
	res, err := m.WithContext(r.ctx).Debug().Where(m.ID.Eq(id)).Updates(&model.CommonConfig{
		IsDeleted: 1,
	})
	if err != nil {
		logx.WithContext(r.ctx).Errorf("Failed to soft delete data: %v", err)
		return res, err
	}
	return res, nil
}

/*
func (r *CommonConfigRepo) ForeverDeleteCommonConfig(id int64) (gen.ResultInfo, error) {
	m := r.svcCtx.Model.CommonConfig
	res, err := m.WithContext(r.ctx).Debug().Where(m.ID.Eq(id)).Delete()
	if err != nil {
		logx.WithContext(r.ctx).Errorf("Failed to hard delete data: %v", err)
		return res, err
	}
	return res, nil
}
*/
